namespace SmartSoft.Controls
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;

    public class TbarSearch : ToolStripTextBox
    {
        private bool ib_DeleteKeys = true;
        private bool ib_Searched;
        private bool ib_SearchFocused;
        private string is_FieldNameSearch = "";
        private Timer oTimerKeyWait;

        public TbarSearch()
        {
            this.Text = "T\x00ecm nhanh...";
            base.ToolTipText = "F4";
            base.GotFocus += new EventHandler(this.TbarSearch_GotFocus);
            base.LostFocus += new EventHandler(this.TbarSearch_LostFocus);
            base.TextChanged += new EventHandler(this.TbarSearch_TextChanged);
        }

        private void KeyWait(object sender, EventArgs e)
        {
            if ((this.ib_DeleteKeys || !this.ib_SearchFocused) || string.IsNullOrEmpty(this.Text))
            {
                this.TimerKeyWait.Stop();
            }
            else
            {
                this.TimerKeyWait.Stop();
                this.SetPosition(this.Text);
            }
        }

        protected override bool ProcessCmdKey(ref Message m, Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                case Keys.Up:
                case Keys.Right:
                case Keys.Down:
                    this.OnLostFocus(EventArgs.Empty);
                    break;

                case Keys.Delete:
                    this.ib_DeleteKeys = true;
                    break;

                case Keys.F3:
                    if (this.ib_SearchFocused)
                    {
                        this.OnLostFocus(EventArgs.Empty);
                    }
                    break;

                case Keys.Back:
                    this.ib_DeleteKeys = true;
                    break;

                case Keys.Return:
                    this.OnLostFocus(EventArgs.Empty);
                    break;

                default:
                    this.ib_DeleteKeys = false;
                    break;
            }
            return base.ProcessCmdKey(ref m, keyData);
        }

        [Browsable(false)]
        private void SetPosition(object ps_SearchedText)
        {
            if (((this.GridBrowserSearch != null) && (this.GridBrowserSearch.Rows.Count > 0)) && (this.ib_SearchFocused && !this.ib_DeleteKeys))
            {
                this.ib_Searched = true;
                int num = 0;
                BindingSource dataSource = this.GridBrowserSearch.DataSource as BindingSource;
                num = dataSource.Find(this.FieldNameSearch, ps_SearchedText);
                if (num < 0)
                {
                    DataView view = null;
                    if (dataSource.DataSource is DataTable)
                    {
                        view = new DataView((DataTable) dataSource.DataSource, this.FieldNameSearch + " LIKE '" + ps_SearchedText.ToString().Replace("'", "''") + "*'", "", DataViewRowState.OriginalRows);
                    }
                    else if (string.IsNullOrEmpty((this.GridBrowserSearch.DataSource as BindingSource).Filter))
                    {
                        view = new DataView(((DataSet) dataSource.DataSource).Tables[0], this.FieldNameSearch + " LIKE '" + ps_SearchedText.ToString().Replace("'", "''") + "*'", "", DataViewRowState.OriginalRows);
                    }
                    else
                    {
                        view = new DataView(((DataSet) dataSource.DataSource).Tables[0], (this.GridBrowserSearch.DataSource as BindingSource).Filter + " AND " + this.FieldNameSearch + " LIKE '" + ps_SearchedText.ToString().Replace("'", "''") + "*'", "", DataViewRowState.OriginalRows);
                    }
                    if (view.Count > 0)
                    {
                        num = dataSource.Find(this.FieldNameSearch, view[0][this.FieldNameSearch]);
                    }
                }
                if (num >= 0)
                {
                    if (num == 0)
                    {
                        dataSource.Position = dataSource.Count - 1;
                        dataSource.Position = num;
                    }
                    else
                    {
                        dataSource.Position = num;
                    }
                }
            }
        }

        private void TbarSearch_GotFocus(object sender, EventArgs e)
        {
            if ((this.GridBrowserSearch != null) && (this.GridBrowserSearch.Rows.Count > 0))
            {
                if (this.GridBrowserSearch.Columns[this.GridBrowserSearch.CurrentCell.ColumnIndex].ValueType != typeof(string))
                {
                    this.GridBrowserSearch.Focus();
                }
                else
                {
                    this.FieldNameSearch = this.GridBrowserSearch.Columns[this.GridBrowserSearch.CurrentCell.ColumnIndex].DataPropertyName;
                    this.Text = "";
                    this.ib_SearchFocused = true;
                    this.ib_DeleteKeys = false;
                }
            }
        }

        private void TbarSearch_LostFocus(object sender, EventArgs e)
        {
            if ((this.GridBrowserSearch != null) && (this.GridBrowserSearch.Rows.Count > 0))
            {
                if (!this.ib_Searched && !string.IsNullOrEmpty(this.Text))
                {
                    this.SetPosition(this.Text);
                }
                this.ib_SearchFocused = false;
                this.ib_DeleteKeys = true;
                this.Text = "T\x00ecm nhanh...";
                this.GridBrowserSearch.Focus();
            }
        }

        private void TbarSearch_TextChanged(object sender, EventArgs e)
        {
            this.ib_Searched = false;
            this.TimerKeyWait.Stop();
            this.TimerKeyWait.Start();
        }

        private string FieldNameSearch
        {
            get
            {
                return this.is_FieldNameSearch;
            }
            set
            {
                this.is_FieldNameSearch = value;
            }
        }

        private DataGridView GridBrowserSearch
        {
            get
            {
                Control parent = base.Parent;
                while ((parent != null) && !(parent is Form))
                {
                    parent = parent.Parent;
                }
                if ((parent != null) && (parent is FormControl))
                {
                    return (parent as FormControl).ActiveGridBrowser;
                }
                return null;
            }
        }

        private Timer TimerKeyWait
        {
            get
            {
                if (this.oTimerKeyWait == null)
                {
                    this.oTimerKeyWait = new Timer();
                    this.oTimerKeyWait.Interval = 250;
                    this.oTimerKeyWait.Stop();
                    this.oTimerKeyWait.Tick += new EventHandler(this.KeyWait);
                }
                return this.oTimerKeyWait;
            }
            set
            {
                if ((value != null) && (value != this.oTimerKeyWait))
                {
                    this.oTimerKeyWait = value;
                }
            }
        }
    }
}

